<?php
/**
 * Created by PhpStorm.
 * User: Administor
 * Date: 2018/9/16
 * Time: 21:57
 */
namespace app\admin\model;
use think\Model;

class Cate extends Model{

    public function cateTree($cateRes){
//        $cateRes=$_cateRes;
        return $this->sort($cateRes);
    }
   public function sort($cateRes,$pid=0,$level=0){
       static $arr=array();
       foreach ($cateRes as $k=>$v){
           if ($v['pid']==$pid){
               $v['level']=$level;
               $arr[]=$v;
               $this->sort($cateRes,$v['id'],$level+1);
           }
       }
       return $arr;
       
   }
    public function childRenids($cateId){
        $data=$this->field('id,pid')->select();
       return $this->_childRenids($data,$cateId);
    }
    //子栏目
    private function _childRenids($data,$cateId){
        static  $arr=array();
        foreach ($data as $k=>$v){
            if ($v['pid']==$cateId){
                $arr[]=$v['id'];
                $this->_childRenids($data,$v['id']);
            }
        }
        return $arr;

    }
    //批量删除
    public function delAll($cateIds){
        foreach ($cateIds as $k=> $v) {
            $childIds[]=$this->childRenids($v);
            $childIds[]=(int)$v;
        }
        $_childIds=array();
        foreach ($childIds  as $k=> $v) {
            if (is_array($v)){
                foreach ($v as $k1=>$v1){
                    $_childIds[]=$v1;
                }
            } else {
                $_childIds[] = $v;
            }
        }
        $_childIds=array_unique($_childIds);
        $this::destroy($_childIds);

    }
}